package com.xyongfeng.foodManagement.controller;


import com.xyongfeng.foodManagement.common.BaseResponse;
import com.xyongfeng.foodManagement.common.ErrorCode;
import com.xyongfeng.foodManagement.common.ResultUtils;
import com.xyongfeng.foodManagement.domain.Permission;
import com.xyongfeng.foodManagement.domain.Role;
import com.xyongfeng.foodManagement.domain.dto.RoleDTO;
import com.xyongfeng.foodManagement.domain.vo.AdminInfoVO;
import com.xyongfeng.foodManagement.domain.vo.RoleVO;
import com.xyongfeng.foodManagement.exception.ThrowUtils;
import com.xyongfeng.foodManagement.service.AdminInfoService;
import com.xyongfeng.foodManagement.service.PermissionService;
import com.xyongfeng.foodManagement.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 角色
 *
 * @author xyongfeng
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {


    @Autowired
    private RoleService roleService;
    @Autowired
    private AdminInfoService adminInfoService;


    @GetMapping
    public BaseResponse<List<Role>> getRoleByAdminId(HttpServletRequest httpRequest) {
        AdminInfoVO loginAdmin = adminInfoService.getLoginAdmin(httpRequest);
        return ResultUtils.success(roleService.getRoleByAdminId(loginAdmin.getAdminId()));
    }

    @PostMapping
    public BaseResponse<String> updateRole(@RequestBody RoleDTO roleDTO, HttpServletRequest httpRequest) {
        AdminInfoVO loginAdmin = adminInfoService.getLoginAdmin(httpRequest);
        ThrowUtils.throwIf(loginAdmin.getAdminId() != 1, ErrorCode.NO_AUTH_ERROR);
        boolean b = roleService.updateRole(roleDTO);
        if (b)
            return ResultUtils.success("提交成功");
        return ResultUtils.error(ErrorCode.OPERATION_ERROR, "提交失败");
    }

    @PostMapping("/{role}")
    public BaseResponse<Role> addRole(@PathVariable String role,HttpServletRequest httpRequest) {
        AdminInfoVO loginAdmin = adminInfoService.getLoginAdmin(httpRequest);
        ThrowUtils.throwIf(loginAdmin.getAdminId() != 1, ErrorCode.NO_AUTH_ERROR);
        return ResultUtils.success(roleService.addRole(role));
    }

    @DeleteMapping("/{roleId}")
    public BaseResponse<String> deleteByRoleId(@PathVariable Integer roleId,HttpServletRequest httpRequest) {
        AdminInfoVO loginAdmin = adminInfoService.getLoginAdmin(httpRequest);
        ThrowUtils.throwIf(loginAdmin.getAdminId() != 1, ErrorCode.NO_AUTH_ERROR);
        boolean b = roleService.deleteByRoleId(roleId);
        if (b)
            return ResultUtils.success("提交成功");
        return ResultUtils.error(ErrorCode.OPERATION_ERROR, "提交失败");
    }


    @GetMapping("/admin/{roleId}")
    public BaseResponse<List<AdminInfoVO>> selectAdminByRoleId(@PathVariable Integer roleId) {
        return ResultUtils.success(roleService.selectAdminByRoleId(roleId));
    }


    @GetMapping("/admin")
    public BaseResponse<List<RoleVO>> selectAllRoleInfo() {
        return ResultUtils.success(roleService.selectAllRoleInfo());
    }


}
